package com.the_ring.chapter03;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.IllegalFormatCodePointException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView resultTv;
    private Button equalBt;

    private String operator = ""; // 运算符
    private String firstNum = ""; // 第一个操作数
    private String secondNum = ""; // 第二个操作数
    private String result = ""; // 当前计算结果

    private boolean tag = true; // 表示 firstNum 是否为空
    private boolean computeTag = true; // 计算是否出错

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            return false;
        }
    });

    public void initView() {
        resultTv = findViewById(R.id.tv_result);
        equalBt = findViewById(R.id.bt_equal);
        equalBt.setOnClickListener(this);

        findViewById(R.id.bt_0).setOnClickListener(this);
        findViewById(R.id.bt_1).setOnClickListener(this);
        findViewById(R.id.bt_2).setOnClickListener(this);
        findViewById(R.id.bt_3).setOnClickListener(this);
        findViewById(R.id.bt_4).setOnClickListener(this);
        findViewById(R.id.bt_5).setOnClickListener(this);
        findViewById(R.id.bt_6).setOnClickListener(this);
        findViewById(R.id.bt_6).setOnClickListener(this);
        findViewById(R.id.bt_7).setOnClickListener(this);
        findViewById(R.id.bt_8).setOnClickListener(this);
        findViewById(R.id.bt_9).setOnClickListener(this);
        findViewById(R.id.bt_ce).setOnClickListener(this);
        findViewById(R.id.bt_times).setOnClickListener(this);
        findViewById(R.id.bt_divide).setOnClickListener(this);
        findViewById(R.id.bt_C).setOnClickListener(this);
        findViewById(R.id.bt_plus).setOnClickListener(this);
        findViewById(R.id.bt_subtract).setOnClickListener(this);
        findViewById(R.id.ib_square).setOnClickListener(this);
        findViewById(R.id.bt_reciprocal).setOnClickListener(this);
        findViewById(R.id.bt_dot).setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        computeTag = true;
        int id = v.getId();

        if (id == R.id.bt_equal) {
            calculate();
        }

        if (id == R.id.bt_0) {
            if (tag) {
                firstNum = firstNum + "0";
            } else {
                secondNum = secondNum + "0";
            }
            result = result + "0";
        } else if (id == R.id.bt_1) {
            if (tag) {
                firstNum = firstNum + "1";
            } else {
                secondNum = secondNum + "1";
            }
            result = result + "1";
        } else if (id == R.id.bt_2) {
            if (tag) {
                firstNum = firstNum + "2";
            } else {
                secondNum = secondNum + "2";
            }
            result = result + "2";
        } else if (id == R.id.bt_3) {
            if (tag) {
                firstNum = firstNum + "3";
            } else {
                secondNum = secondNum + "3";
            }
            result = result + "3";
        } else if (id == R.id.bt_4) {
            if (tag) {
                firstNum = firstNum + "4";
            } else {
                secondNum = secondNum + "4";
            }
            result = result + "4";
        } else if (id == R.id.bt_5) {
            if (tag) {
                firstNum = firstNum + "5";
            } else {
                secondNum = secondNum + "5";
            }
            result = result + "5";
        } else if (id == R.id.bt_6) {
            if (tag) {
                firstNum = firstNum + "6";
            } else {
                secondNum = secondNum + "6";
            }
            result = result + "6";
        } else if (id == R.id.bt_7) {
            if (tag) {
                firstNum = firstNum + "7";
            } else {
                secondNum = secondNum + "7";
            }
            result = result + "7";
        } else if (id == R.id.bt_8) {
            if (tag) {
                firstNum = firstNum + "8";
            } else {
                secondNum = secondNum + "8";
            }
            result = result + "8";
        } else if (id == R.id.bt_9) {
            if (tag) {
                firstNum = firstNum + "9";
            } else {
                secondNum = secondNum + "9";
            }
            result = result + "9";
        } else if (id == R.id.bt_divide) {
            if (firstNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            if (tag) {
                tag = !tag;
            } else {
                equalBt.performClick();
            }
            if (computeTag) {
                operator = "÷";
                result = result + "÷";
            }
        } else if (id == R.id.bt_times) {
            if (firstNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            if (tag) {
                tag = !tag;
            } else {
                equalBt.performClick();
            }
            if (computeTag) {
                operator = "×";
                result = result + "×";
            }
        } else if (id == R.id.bt_plus) {
            if (firstNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            if (tag) {
                tag = !tag;
            } else {
                equalBt.performClick();
            }
            if (computeTag) {
                operator = "+";
                result = result + "+";
            }
        } else if (id == R.id.bt_subtract) {
            if (tag) {
                tag = !tag;
            } else {
                equalBt.performClick();
            }
            if (computeTag) {
                operator = "-";
                result = result + "-";
            }
        } else if (id == R.id.ib_square) {
            operator = "√";
            equalBt.performClick();
        } else if (id == R.id.bt_reciprocal) {
            operator = "/";
            equalBt.performClick();
        } else if (id == R.id.bt_ce) {

        } else if (id == R.id.bt_C) {
            firstNum = "";
            secondNum = "";
            operator = "";
            tag = true;
            result = "";
        }

        resultTv.setText(result);
    }

    private void calculate() {
        double calculateResult = 0;
        if (operator.equals("")) {
            Toast.makeText(this, "请输入运算符", Toast.LENGTH_SHORT).show();
            computeTag = false;
            return;
        } else if (operator.equals("√")) {
            if (firstNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            } else if (Double.parseDouble(firstNum) < 0) {
                Toast.makeText(this, "开方数字不能小于零", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            }
            result = result + "√";
            calculateResult = Math.sqrt(Double.parseDouble(firstNum));
        } else if (operator.equals("/")) {
            if (firstNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            } else if (Double.parseDouble(firstNum) == 0) {
                Toast.makeText(this, "不能对零求导数", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            }
            result = result + "/";
            calculateResult = 1 / Double.parseDouble(firstNum);
        } else if (operator.equals("+")) {
            if (firstNum.equals("") || secondNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            }
            calculateResult = Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
        } else if (operator.equals("-")) {
            if (secondNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            }
            if (firstNum.equals("")) {
                firstNum = "-" + secondNum;
                operator = "";
                secondNum = "";
                tag = false;
                return;
            }
            calculateResult = Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
        } else if (operator.equals("×")) {
            if (firstNum.equals("") || secondNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            }
            calculateResult = Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
        } else if (operator.equals("÷")) {
            if (firstNum.equals("") || secondNum.equals("")) {
                Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            } else if (Double.parseDouble(secondNum) == 0) {
                Toast.makeText(this, "除数不能为零", Toast.LENGTH_SHORT).show();
                computeTag = false;
                return;
            }
            calculateResult = Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
        tag = true;
        operator = "";
        firstNum = String.valueOf(calculateResult);
        secondNum = "";
        result = result + "=" + firstNum;
    }
}